package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.Ad;
import com.dotmedia.entities.Advertiser;

/**
 * This class is the database operation class for dealing with Advertiser entity
 * 
 * @author siddharthsaha
 * 
 */
public class AdvertiserDatabaseOperations {

	private static Logger logger = Logger.getLogger("AdvertiserDatabaseOperation");

	/**
	 * This method persists the advertiser entity to the database
	 * 
	 * @param advertiserEntity
	 *        The Advertiser entity which is to be persisted in the database
	 */
	public void persistAdvertiserEntity(Advertiser advertiserEntity) {
		logger.info("IN method persistAdvertiserEntity() in AdvertiserDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(advertiserEntity);
		if (persistStatus == 0) {
			logger.info("The advertiser entity with the id: " + advertiserEntity.getAdvertiserId() + " was successfully persisted");
		} else {
			logger.error("There was an error when persisting the advertiser entity");
		}
		logger.info("OUT method persistAdvertiserEntity() in AdvertiserDatabaseOperations");
	}

	public void removeAdvertiserEntity(Advertiser advertiserEntity) {
		logger.info("IN method removeAdvertiserEntity() in AdvertiserDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		logger.info("Removing an advertiser entity from the database from the id: " + advertiserEntity.getAdvertiserId());
		int removeStatus = databaseAccessService.removeEntity(advertiserEntity, advertiserEntity.getAdvertiserId().intValue());
		if (removeStatus == 0) {
			logger.info("The advertiser entity was successfully reoved from the database");
		} else {
			logger.error("There was an error. The advertiser entity could not be successfully removed from the database");
		}
		logger.info("OUT method removeAdvertiserEntity() in AdvertiserDatabaseOperations");
	}

	public void updateAdvertiserEntity(Advertiser advertiserEntity) {
		logger.info("IN method updateAdvertiserEntity() in AdvertiserDatabaseOperation");
		String query = "SELECT advertiserEntity FROM Advertiser advertiserEntity WHERE advertiserEntity.system.id = "
				+ advertiserEntity.getSystem().getId() + " AND advertiserEntity.advertiserRefCode = '" + advertiserEntity.getAdvertiserRefCode()
				+ "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retrievedEntities.size(); index++) {
			Advertiser currentAdvertiserEntity = (Advertiser) retrievedEntities.get(index);
			if (currentAdvertiserEntity.getSystem().getId().intValue() == advertiserEntity.getSystem().getId().intValue()
					&& currentAdvertiserEntity.getAdvertiserRefCode().equals(advertiserEntity.getAdvertiserRefCode())) {
				logger.info("Obtained the entity to be updated");
				currentAdvertiserEntity.setDescription(advertiserEntity.getDescription());
				currentAdvertiserEntity.setIsAgency(advertiserEntity.getIsAgency());
				currentAdvertiserEntity.setStatus(advertiserEntity.getStatus());
				int updateStatus = databaseAccessService.updateEntity(currentAdvertiserEntity);
				if (updateStatus == 0) {
					logger.info("Advertiser entity was successfully updated");
					updated = false;
				} else {
					logger.error("There was some error in updating the advertiser entity");
				}
				break;
			}
		}
		if (updated) {
			logger.info("Advetiser entity was successfully updated");
		}
		logger.info("OUt method updateAdvertiserEntity() in AdvertiserDatabaseOperations");
	}

	public List<Advertiser> getAllAdvertiserEntities() {
		logger.info("IN method getAllAdvertiserEntities() in AdvertiserDatabaseOperations");
		List<Advertiser> toReturnList = new ArrayList<Advertiser>();
		String query = "SELECT advertiserEntity FROM Advertiser advertiserEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedList = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrievedList.size(); index++) {
			Advertiser currentAdvertiserEntity = (Advertiser) retrievedList.get(index);
			toReturnList.add(currentAdvertiserEntity);
		}
		logger.info("OUt method getAllAdvertiserEntities() in AdvertiserDatabaseOperations");
		return toReturnList;
	}

	public List<Advertiser> getAdvertiserEntitiesByAdvertiserId(Integer advertiserId) {
		logger.info("IN method getAdvertiserEntitiesByAdvertiserId() in AdvertiserDatabaseOperations");
		List<Advertiser> toReturnList = new ArrayList<Advertiser>();
		String query = "SELECT advertiserEntity FROM Advertiser advertiserEntity WHERE advertiserEntity.advertiserId = " + advertiserId.intValue();
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Advertiser currentAdvertiserEntity = (Advertiser) retrivedEntities.get(index);
			toReturnList.add(currentAdvertiserEntity);
		}
		logger.info("OUT method getAdvertiserEntitiesByAdvertiserId() in AdvertiserDatabaseOperations");
		return toReturnList;
	}

	public List<Advertiser> getAdvertiserEntitiesByAdvertiserRefCode(String advertiserRefCode) {
		logger.info("IN method getAdvertiserEntitiesByAdvertiserRefCode() in AdvertiserDatabaseOperations");
		List<Advertiser> toReturnList = new ArrayList<Advertiser>();
		String query = "SELECT advertiserEntity FROM Advertiser advertiserEntity WHERE advertiserEntity.advertiserRefCode = '" + advertiserRefCode
				+ "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedEntities.size(); index++) {
			Advertiser currentAdvertiserEntity = (Advertiser) retreivedEntities.get(index);
			toReturnList.add(currentAdvertiserEntity);
		}
		logger.info("OUT method getAdvertiserEntitiesByAdvertiserRefCode() in AdvertiserDatabaseOperations");
		return toReturnList;
	}

	/**
	 * This method returns the list of Advertiser entities whise isAgency status is what is passed to the method - true / false
	 * 
	 * @param isAgency
	 * @return
	 */
	public List<Advertiser> getAdvertiserEntitiesWithAgencyStatus(boolean isAgency) {
		logger.info("IN method getAdvertiserEntitiesWithAgencyStatus() in AdvertiserDatabaseOperations");
		List<Advertiser> toReturnList = new ArrayList<Advertiser>();
		String query = "SELECT advertiserEntity FROM Advertiser advertiserEntity WHERE advertiserEntity.isAgency = " + isAgency;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			Advertiser currentAdvertiserEntity = (Advertiser) retrivedEntities.get(index);
			if (currentAdvertiserEntity.getIsAgency().booleanValue() == isAgency) {
				toReturnList.add(currentAdvertiserEntity);
			}
		}
		logger.info("OUT methos getAdvertiserEntitiesWithAgencyStatus() in AdvertiserDatabaseOperations");
		return toReturnList;
	}

}
